<template>
  <div>
    <!-- 面包导航 -->
    <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--公告列表卡片-->
    <el-card class="box-card card" id="card">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item size="mini" label="公告标题" label-width="70px" style="font-weight: bold">
          <el-input clearable v-model="searchForm.noticeTip" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="公告正文" label-width="70px" style="font-weight: bold">
          <el-input clearable v-model="searchForm.noticeMessage" placeholder="请输入正文"></el-input>
        </el-form-item>
        <el-form-item size="mini" style="margin-left: 10px;">
          <el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
          <el-button icon="el-icon-search" @click="onSubmit(1)" type="primary">查询</el-button>
          <el-button icon="el-icon-plus" @click="show" type="success">添加</el-button>
        </el-form-item>
      </el-form>
      <!--表格内容显示区域-->
      <el-table
        :max-height="this.getTableMaxHeight()"
        v-loading="loading" element-loading-text="数据加载中..."
        :data="page.records"
        border
        size="mini"
        style="width: 100%;">
        <el-table-column
          width="60px"
          align="center"
          type="index"
          label="序号">
        </el-table-column>
        <el-table-column
          width="150px"
          align="center"
          prop="noticeTip"
          label="公告标题">
        </el-table-column>
        <el-table-column
          width="600px"
          align="center"
          prop="noticeMessage"
          label="公告正文">
        </el-table-column>
        <el-table-column
          width="200px"
          align="center"
          prop="createTime"
          label="创建时间">
        </el-table-column>
        <el-table-column
          width="200px"
          align="center"
          sortable
          prop="modifiedTime"
          label="更新时间">
        </el-table-column>
        <el-table-column
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑公告" placement="top" :enterable="false">
              <el-button @click="show(scope.row.id, true)" size="mini" type="primary" icon="el-icon-edit"
                         circle></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除公告" placement="top" :enterable="false">
              <el-button @click="deleteAccount(scope.row.id)" size="mini" type="danger" icon="el-icon-delete"
                         circle></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        v-if="page.total > 0"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </el-card>
    <!--用户添加/修改模态框 :userData="isEdit === true ? userData : null"-->
    <AddOrUpdate :addOrUpdateVisible="addOrUpdateVisible"
                 :isEdit="isEdit"
                 :id="id"
                 @dataUpdate="init"
                 @changeShow="showAddOrUpdate">
    </AddOrUpdate>
  </div>
</template>

<script>
import { getNoticeListPage, deleteNotice, searchNoticeListPage } from '@/api/notice'
import AddOrUpdate from './AddOrUpdate'
  export default {
    name: 'Notice',
    data () {
      return {
        searchForm: {
          noticeTip: '',
          noticeMessage: ''
        },
        page: {
          records: [],
          total: 0,
          size: 0,
          current: 0,
          orders: []
        },
        pageNum: 1,
        pageSize: this.getPageSize(),
        // 控制新增编辑弹窗的显示与隐藏
        addOrUpdateVisible: false,
        isEdit: false,
        id: 0,
        loading: false,
      }
    },
    components: {
      AddOrUpdate
    },
    created () {
      this.init()
    },
    methods: {
      init () {
        const _this = this
        _this.loading = true
          /* 创建组件的时候加载页面信息 */
          getNoticeListPage(_this.pageNum, _this.pageSize).then(response => {
            _this.page = response.data
          }).finally(() => {
          _this.loading = false
        })
      },
      resetForm () {
        this.searchForm.noticeTip = ''
        this.searchForm.noticeMessage = ''
        this.pageNum = 1
        this.pageSize = this.getPageSize()
        const _this = this
        getNoticeListPage(this.pageNum, this.pageSize).then(response => {
          _this.page = response.data
        })
      },
      onSubmit (page = this.pageNum) {
        const _this = this
        searchNoticeListPage(this.searchForm, page, this.pageSize).then(response => {
          _this.page = response.data
        })
      },
      // 按钮点击事件 显示新增编辑弹窗组件
      show (id, edit) {
        this.addOrUpdateVisible = true
        if (edit) {
          // 如果是编辑框，将绑定的数据传递给子组件
          this.id = id
        }
        this.isEdit = edit
      },
      // 监听 子组件弹窗关闭后触发，有子组件调用
      showAddOrUpdate (data) {
        this.addOrUpdateVisible = data !== 'false'
      },
      deleteAccount (id) {
        this.$confirm('是否删除该公告?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const _this = this
          deleteNotice(id).then(response => {
            _this.init()
            _this.RightMessage(_this, '成功', response.message, 'success')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除公告'
          })
        })
      },
      handleSizeChange (val) {
        /* 将value赋值给size, 重新查询数据 */
        this.pageSize = val
        this.onSubmit()
      },
      handleCurrentChange (val) {
        this.pageNum = val
        this.onSubmit()
      },
    },
    mounted () {
      // 获取屏幕高度
      document.getElementById('card').style.height = this.getWindowHeight()
    }
  }
</script>

<style scoped>

</style>
